/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.painting.resources;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.hercules.android.receiver.model.objects.XRERectangle;

/**
 * Painter for {@link XRERectangle} resource. Just paints rectangle with border to surface.
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> Not thread safe, because base class is not thread safe
 *
 * @author 7realm
 * @version 1.0
 */
public class XRERectanglePainter extends BaseXREResourcePainter {
    /**
     * Constructor for class {@link XRERectanglePainter}.
     *
     * @param xreResource the {@link XRERectangle}
     */
    public XRERectanglePainter(XRERectangle xreResource) {
        super(xreResource);
    }

    /**
     * Draw resource on surface.
     *
     * @param canvas the canvas to be used
     */
    @Override
    public void onDraw(Canvas canvas) {
        // get XRERectangle
        XRERectangle rectangle = (XRERectangle) getXREResource();
        if (getXREView().getDimensions() != null) {
            // fill rectangle
            int width = getXREView().getWidth();
            int height = getXREView().getHeight();

            // get border
            float border = (float) rectangle.getBorderThickness();

            if (border > 0.0000000000001) {
                // create the Paint object with specified border color
            	Paint paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(rectangle.getBorderColor());
                paint.setStrokeWidth(border);

                // paint border
                canvas.drawRect(border / 2, border / 2, width - border, height - border, paint);
                
                paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(rectangle.getColor());
                
                canvas.drawRect(border, border, width - 2 * border, height - 2 * border, paint);
            } else {
            	Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(rectangle.getColor());
                
                canvas.drawRect(0, 0, width, height, paint);
            }
        }
    }
}
